/**
 * @author ccpalu
 */

function createSegLabelForm(labels){
/* Fields in the configForm, listing all the labels of each column of a file uploaded by a user.
 * It allows the user to do the correspondence of columns and necessary data, for the browser.
 */
	var topBox = 1, intervalBox = 36,  topLabel = 4.5, intervalLabel = 35.5;
	segFileBG();
	document.getElementById('closeConfigForm').style.display='none';

	var configForm = document.createElement('FORM');
	configForm.id='configForm2';
	setPosition(configForm, 15,185);
	configForm.style.zIndex="403";
	document.getElementById('configDiv').appendChild(configForm);

	var sampleIdSelect = document.createElement('SELECT');
	sampleIdSelect.id='sampleIdSelect';
	configForm.appendChild(sampleIdSelect);
	sampleIdSelect.style.width="150px";
	setFont(sampleIdSelect,'15px',0,'center',0);
	setPosition(sampleIdSelect, 140, topBox, 'absolute');
	
	var sampleIdLabel=createParagraph('sampleIdLabel', 'Sample ID', configForm);
	sampleIdLabel.style.width="115px";
	setFont(sampleIdLabel,'15px',0,'right',0);
	configForm.appendChild(sampleIdLabel);
	setPosition(sampleIdLabel, 15, topLabel, 'absolute');

	var sampleChromosomeSelect= document.createElement('SELECT');
	sampleChromosomeSelect.id='sampleChromosomeSelect';
	sampleChromosomeSelect.style.width="150px";
	setFont(sampleChromosomeSelect,'15px',0,'center',0);
	configForm.appendChild(sampleChromosomeSelect);
	setPosition(sampleChromosomeSelect, 140, topBox+intervalBox, 'absolute');
	
	var sampleChromosomeLabel=createParagraph('sampleChromosomeLabel', 'Chromosome', configForm);
	sampleChromosomeLabel.style.width="115px";
	setFont(sampleChromosomeLabel,'15px',0,'right',0);
	configForm.appendChild(sampleChromosomeLabel);
	setPosition(sampleChromosomeLabel, 15, topLabel+intervalLabel, 'absolute');	

	var segmentStartSelect= document.createElement('SELECT');
	segmentStartSelect.id='segmentStartSelect';
	segmentStartSelect.style.width="150px";
	setFont(segmentStartSelect,'15px',0,'center',0);
	configForm.appendChild(segmentStartSelect);
	setPosition(segmentStartSelect, 140, topBox+intervalBox*2, 'absolute');

	var segmentStartSelectLabel=createParagraph('segmentStartSelectLabel', "Segment's start", configForm);
	segmentStartSelectLabel.style.width="115px";
	setFont(segmentStartSelectLabel,'15px',0,'right',0);
	configForm.appendChild(segmentStartSelectLabel);
	setPosition(segmentStartSelectLabel, 15, topLabel+intervalLabel*2, 'absolute');	
	
	var segmentEndSelect = document.createElement('SELECT');
	segmentEndSelect.id='segmentEndSelect';
	segmentEndSelect.style.width="150px";
	setFont(segmentEndSelect,'15px',0,'center',0);
	configForm.appendChild(segmentEndSelect);
	setPosition(segmentEndSelect, 140, topLabel+intervalLabel*3, 'absolute');
	
	var segmentEndLabel=createParagraph('segmentEndLabel', "Segment's end", configForm);
	segmentEndLabel.style.width="115px";
	setFont(segmentEndLabel,'15px',0,'right',0);
	configForm.appendChild(segmentEndLabel);
	setPosition(segmentEndLabel, 15, topLabel+intervalLabel*3, 'absolute');
	
	var copyNumberSelect = document.createElement('SELECT');
	copyNumberSelect.id='copyNumberSelect';
	copyNumberSelect.style.width="150px";
	setFont(copyNumberSelect,'15px',0,'center',0);
	configForm.appendChild(copyNumberSelect);
	setPosition(copyNumberSelect, 140, topBox+intervalBox*4, 'absolute');
	
	var copyNumberLabel=createParagraph('copyNumberLabel', "Copy Number\nvalue", configForm);
	copyNumberLabel.style.width="115px";
	setFont(copyNumberLabel,'15px',0,'right',0);
	configForm.appendChild(copyNumberLabel);
	setPosition(copyNumberLabel, 15, topLabel+intervalLabel*4-7, 'absolute');

	for (var i = 0; i<labels.length;i=i+1){
		sampleIdSelect.options[i] = new Option(labels[i], i);
		sampleChromosomeSelect.options[i] = new Option(labels[i], i);
		segmentStartSelect.options[i] = new Option(labels[i], i);
		segmentEndSelect.options[i] = new Option(labels[i], i);
		copyNumberSelect.options[i] = new Option(labels[i], i);
	}

	sampleIdSelect.selectedIndex = 0;
	sampleChromosomeSelect.selectedIndex =1;
	segmentStartSelect.selectedIndex = 2;
	segmentEndSelect.selectedIndex = 3;
	copyNumberSelect.selectedIndex = labels.length-1;
	
	var cnNotationSelect = document.createElement('SELECT');
	cnNotationSelect.id='cnNotationSelect';
	cnNotationSelect.style.width="150px";
	setFont(cnNotationSelect,'15px',0,'center',0);
	configForm.appendChild(cnNotationSelect);
	setPosition(cnNotationSelect, 140, topBox+intervalBox*5, 'absolute');
	
	var cnNotationLabel=createParagraph('cnNotationLabel', 'Copy Number\nScale', configForm);
	cnNotationLabel.style.width="115px";
	setFont(cnNotationLabel,'15px',0,'right',0);
	configForm.appendChild(cnNotationLabel);
	setPosition(cnNotationLabel, 15, topLabel+intervalLabel*5 -7, 'absolute');
	
	var notations = ['select', "linear scale", 'log2'+"'copy number'", 'log2'+"('copy number' / 2)"];
		
	for (i = 0; i< notations.length; i= i+1){
		cnNotationSelect.options[i] = new Option(notations[i], i);
	}
	
	var submitLabels = document.createElement('INPUT');
	submitLabels.type='button';
	submitLabels.id='submitLabels';
	submitLabels.name='submitLabels';
	submitLabels.value='Submit';
	submitLabels.onclick=checkCnLabel;
	configForm.appendChild(submitLabels);
	setPosition(submitLabels, 327, topBox+intervalBox*6, 'absolute');
	
	return false;
}

function segFileBG(){
	var w1=1.5, h1=1.9;
	var w = w1*300, h= h1*250 -100;

	if(document.docType){
		var segFileOptionsDiv = document.createElement('div');
		segFileOptionsDiv.id='segFileOptionsDiv';
		segFileOptionsDiv.style.width=w+'px';
		segFileOptionsDiv.style.height=h+'px';
		segFileOptionsDiv.style.zIndex="402";
		setPosition(segFileOptionsDiv, 0.5,100);
		document.getElementById('configDiv').appendChild(segFileOptionsDiv);
		
		var surface = dojox.gfx.createSurface(segFileOptionsDiv, w, h);
	
		var rect1 = {
			x: 0,
			y: 0,
			width: w*w1,
			height: h*h1,
			r: 10
		};
		var lg1 = {
			type: "linear",
			x1: 100,
			y1: 100,
			x2: 100,
			y2: 200*h1,
			x3: 70*w1,
			y3: 300*h1,
			x4: 300*w1,
			y4: 400*h1,
			//x5: 500*w1,
			//y5: 300*h1,
			
			colors: [{
				offset: 0.1,
				color: "#b3ce7c"
			},
			{
				offset: 0.6,
				color: "#b1cc7a"
			},
			{
				offset: 0.9,
				color: "#dde879"
			},
					{
				offset: 1,
				color: "rgba(255,255,255,0)"
			}]
		};
	
		var vDetail = surface.createRect(rect1).setFill(lg1).setTransform({
			dx: 3,
			dy: 0
		});
		vDetail.getEventSource().setAttribute('id','segFileOptionsVRectDetail');
	}
	else{ //April 2011
		var configBg= document.createElement("img");
		configBg.src = "Images/bgMenu.png";
		configBg.id = "segFileOptionsDiv";
		document.getElementById('configDiv').appendChild(configBg);
		configBg.width=w;
		configBg.height=500;
		setPosition("segFileOptionsDiv", 1,-10);
	}
	return false;
}

function checkCnLabel(){
	var labelsSelect = checkSelectedLabelIndex();
	if (labelsSelect) {
		try {
			loadingStatus("Reading file ...");
			segCollection(labelsSelect);
			uploadFinished(true);
			closeConfigBox();
		} 
		catch (e) {
			removeWrongEntry();
			loadingStatus("Waiting user answer ...");
			if (confirm('There was a problem while reading your file\n'+
				"Press 'OK' for re-check your file's Label correspondence\n"+
				"or\nPress 'Cancel' to submit a new file.")) {
				//graphConfigBox();
			}
			else{
				//loadingStatus("Waiting input data ...");
				remove_element_by_id('segFileOptionsDiv');
				remove_element_by_id('configForm2');
				uploadFinished();
				closeConfigBox();
			}
		}
	}
	return false;
}

function removeWrongEntry(){
// Removing any entry from the file with problem.

	var varSize = document.getElementById('choose_sample').options.length-1;
	document.getElementById('choose_sample').options.length = varSize;
	var segData = document.getElementById('CNV').segData;
	document.getElementById('CNV').index = varSize-1;

	for (var i = 1; i<23 ; i=i+1 ) 	{
		try{
			segData[i].length = varSize-1;
		}
		catch(e){}
	}
	return false;
}
	
function checkSelectedLabelIndex(){
	var labelsSelect = [document.getElementById('sampleIdSelect').selectedIndex, 
		document.getElementById('sampleChromosomeSelect').selectedIndex,
		document.getElementById('segmentStartSelect').selectedIndex, 
		document.getElementById('segmentEndSelect').selectedIndex, 
		document.getElementById('copyNumberSelect').selectedIndex];
	
	for (var i=0; i<5; i=i+1){
		for (var j = i + 1; j < 5; j = j + 1) {
			if (labelsSelect[i]==labelsSelect[j]){
				alert("You can't choose the same label for different variables");
				if (document.getElementById('cnNotationSelect').selectedIndex == 0) {
					alert('Please select the scale method used.');
				}
				return false;
			}
		}
	
	}
	if (document.getElementById('cnNotationSelect').selectedIndex==0){
		alert('Please select the scale method used.');
		return false;
	}
	return labelsSelect;
}
